//
//  RightsManagementController.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/7/1.
//  Copyright © 2020 lshenrong. All rights reserved.
//

#import "RightsManagementController.h"
#import "RightsManageCell.h"

@interface RightsManagementController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UILabel *titleLblFirst;
@property (nonatomic,assign) BOOL unFold;

@property (nonatomic,strong) NSArray *foldArr;
@property (nonatomic,strong) NSArray *unFoldArr;

@property (nonatomic,strong) UILabel *titleLbl;
@property (nonatomic,strong) UISwitch *switchView;

@end

@implementation RightsManagementController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"调度员";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(self.view);
    }];
    self.foldArr =@[@"订单管理",@"任务管理",@"车辆调度",@"顺风车",@"财务管理",@"信息管理",@"我的待办",@"",@""];
    self.unFoldArr =  @[@"订单管理",@"任务管理",@"车辆调度",@"顺风车",@"财务管理",@"财务管理子集1",@"财务管理子集2",@"信息管理",@"我的待办"];

}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //section头部高度
    return 64.00f;
}

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
     if (section == 4) {
         if (self.unFold == YES) {
             return 2;
         }else{
             return 0;
         }
     }
    return 0;
}

//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
       UIControl *view = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
       view.tag = 1000 + section;
       view.backgroundColor = kWhiteColor;
       [view addTarget:self action:@selector(sectionClick:) forControlEvents:UIControlEventTouchUpInside];
    [self setupHeaderView:view section:section];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RightsManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RightsManageCellIdentifier"];
//    if (self.unFold == NO) {
//        [cell updateTitle:self.foldArr[indexPath.row]];
//    }else {
//        [cell updateTitle:self.unFoldArr[indexPath.row]];
//    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 4) {
        NSIndexSet * index = [NSIndexSet indexSetWithIndex:0];

        [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     
     return  64;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return   7;
}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (self.unFold == NO) {
//        return 7;
//    }else {
//        return 9;
//    }
//}


-(void)sectionClick:(UIControl *)view{
    
    //获取点击的组
    NSInteger i = view.tag - 1000;
    //取反
    self.unFold = !self.unFold;
    if (i == 4) {
         //刷新列表
           NSIndexSet * index = [NSIndexSet indexSetWithIndex:4];
           [self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
    }
   
}
- (void)setupHeaderView:(UIView *)contentView section:(NSInteger)section {
    
    UILabel *lable = [UILabel new];
    lable.text =  self.foldArr[section];
    UISwitch *switchView = [[UISwitch alloc] init];
    
    UIView *vLineView =  [[UIView alloc] init];
    vLineView.backgroundColor = kSeparateLineColor;

    [contentView addSubview:lable];
    [contentView addSubview:switchView ];
    [contentView addSubview:vLineView];

    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView).mas_offset(kMarginOffsetX);
        make.centerY.mas_equalTo(contentView);
    }];
    
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(contentView).mas_offset(-kMarginOffsetX);
        make.centerY.mas_equalTo(contentView);
    }];
    
    [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentView).mas_offset(10);
             make.bottom.equalTo(contentView).mas_offset(-1);
             make.height.mas_offset(0.5);
    }];
}






- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UIConfigure tableViewFromFactory];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 64;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        _tableView.tableHeaderView.backgroundColor = kRedColor;
        [_tableView registerClass:[RightsManageCell class] forCellReuseIdentifier:@"RightsManageCellIdentifier"];
    }
    return _tableView;
}

- (UISwitch *)switchView{
    if (!_switchView) {
        _switchView =  [[UISwitch alloc]init];
        _switchView.onTintColor =  kBlueColor;
        [_switchView setOn:YES animated:NO];
        [_switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}


MACRO_UILabelWithPFCGFont(titleLblFirst,@"权限管理",kGrayColor,15)

MACRO_UILabelWithPFCGFont(titleLbl, @"权限标题",kBlackColor, 14)

@end
